﻿var askform_form;
$(function () {
    $(document).bind("contextmenu",function(){return false;});  
    //$(document).bind("selectstart",function(){return false;}); 
    if (typeof askform_option != 'undefined') {
        askform_form = new $.askform($("form[action*='Form']"), askform_option);
    } else {
        askform_form = new $.askform($("form[action*='Form']"));
    }
    var queryString = askform_form.action.substring(askform_form.action.indexOf("?"));
    $("input[type='file']").each(function (index, f) {
        var widget = $(f).parents(".formwidget");
        var maxlength = "10240K";
        if (widget.data("max")) {
            maxlength = widget.data("max") + "K";
        }
        var fileType = widget.data("pattern");
        if (fileType) {
            fileType = fileType.replace(/\(/ig, "").replace(/\)/ig, "").replace(/\|/ig, ";").replace(/\\/ig, "*").replace(/\$/ig, "");
        } else {
            fileType = '*.gif;*.jpg;*.jpeg;*.png;*.bmp;.doc;.docx;.xls;.xlsx;.txt;.ppt;.pptx;.pdf;.rar;.zip;.gzip;';
        }
        $(f).uploadify({
            width: 100,
            height: 26,
            swf: '/script/uploadify.swf', //[*]swf的路径
            uploader: '/upload.ashx' + queryString, //[*]一般处理程序
            cancelImg: '/css/images/uploadify-cancel.png', //取消图片路径
            multi: false,
            removeCompleted: true,
            fileTypeDesc: '文件格式',
            fileTypeExts: fileType, //'*.gif;*.jpg;*.jpeg;*.png;*.bmp;.doc;.docx;.xls;.xlsx;.txt;.ppt;.pptx;.pdf;.rar;.zip;.gzip;', //允许上传的文件类型，使用分号(”;)”分割 例如:*.jpg;*.gif,默认为null(所有文件类型)
            fileSizeLimit: maxlength,
            onUploadSuccess: function (file, data, response) {//上传完成时触发（每个文件触发一次）
                if (data.indexOf('错误提示') > -1) {
                    alert(data);
                }
                else {
                    $("#" + this.settings.id.replace(/_/ig, "\\.")).val(data);
                }
            },
            onUploadError: function (file, errorCode, errorMsg, errorString) {//当单个文件上传出错时触发
                alert('文件：' + file.name + ' 上传失败: ' + errorString);
            },
            onCancel: function (file) {
                $("#" + this.settings.id.replace(/_/ig, "\\.")).val("");
            },
            buttonText: '选择文件'
        });
    });
    $.each(askform_form.widgets, function (index, w) {
        if (w.type == "slide") {
            w.dom.find("input").selectToUISlider();
        }
        else if (w.type == "date") {
            $("input", w.dom).datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, yearRange: '1900:2050' });
        }
        else if (w.type == "box") {
            $("input[type='date']", w.dom).datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, yearRange: '1900:2050' });
        }
        else if (w.type == "order") {
            $("input", w.dom).hide();
            var rightOrder = $('<ul class="fieldorder right"><span class="ordertip">请从左边拖拽到这里排序</span></ul>');
            var map = [];
            $("li input:checked", w.dom).each(function (index, item) {
                var sort = Number($("input[type='text']", $(item).parent()).val());
                map[sort] = $(item);
            });
            for (var i = 0; i < map.length; i++) {
                if (map[i]) {
                    rightOrder.append(map[i].parent());
                }
            }
            $(".fieldbody", w.dom).append(rightOrder);
            $("ul", w.dom).sortable({
                connectWith: "ul",
                update: function (event, ui) {
                    $(".ordertip", w.dom).hide();
                    var holder = ui.item.parent();
                    if (holder.hasClass("left")) {
                        $("input[type='text']", ui.item).val("");
                        $("input[type='checkbox']", ui.item).removeAttr("checked");
                    }
                    
                    $("ul.right li", w.dom).each(function (index, item) {
                        $("input[type='text']", item).val(index);
                        $("input[type='checkbox']", item).attr("checked", "checked");
                    });
                    if ($("ul.right li", w.dom).length == 0) {
                        $(".ordertip", w.dom).show();
                    }
                }
            }).disableSelection();
        }
        else if (w.type == "cascade") {
            var cascades = w.dom.find("select");
            var first = cascades[0];
            var last = cascades[cascades.length - 1];
            for (var i = 0; i < cascades.length - 1; i++) {
                $(cascades[i]).bind("change", function () {
                    var deep = Number($(this).attr("deep"));
                    var parentID = $(this).val();
                    var requestUrl = "/CascadeHandler.ashx";
                    var data = { "ChoiceID": parentID, "timespan": Math.random() };
                    $.getJSON(requestUrl, data, function (json) {
                        var cas = $(cascades[deep + 1]);
                        var first = $(cas.find("option:first-child"));
                        cas.html("<option value=''>" + first.html() + "</option>");
                        for (var j = 0; j < json.length; j++) {
                            cas.append("<option value=" + json[j].ChoiceID + ">" + json[j].Name + "</option>");
                        }
                        try {
                            cas.selectmenu("refresh");
                        } catch (err) { }
                        cas.trigger("change");
                    });

                });
            }
        }
    });
    askform_form.dom.show();
});